package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.ILoginService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @ClassName: LoginController
 * @Author: 南风
 * @Email: 1480207032@qq.com
 * @Description:
 * @Date: 2020/7/11 16:48
 */
@RestController
@RequestMapping("user")
@Anoymous
public class LoginController {
    @Reference(check = false)
    private ILoginService loginService;

    @Reference(check = false)
    private IKaptchaService kaptchaService;

    @PostMapping("login")
    public ResponseData login(@RequestBody Map<String, String> map,
                              HttpServletRequest request,
                              HttpServletResponse response){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        // 1、验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        // kaptcha_uuid
        String kaptcha_uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(captcha);

        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        if (!kaptchaCodeResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 验证失败
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        // 2、用户登录
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);

        UserLoginResponse userLoginResponse = loginService.login(userLoginRequest);
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            // 将token 放到 cookie 里
            //Cookie cookie = new Cookie("access_token", userLoginResponse.getToken());
            //CookieUtil.setCookie(response, cookie);
            Cookie cookie = CookieUtil.genCookie(TokenIntercepter.ACCESS_TOKEN, userLoginResponse.getToken(), "/",
                    24 * 60 * 60);
            cookie.setHttpOnly(true);
            CookieUtil.setCookie(response, cookie);

            return new ResponseUtil<>().setData(userLoginResponse);
        }

        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }

    @GetMapping("login")
    public ResponseData auth(HttpServletRequest request){
        CheckAuthRequest authRequest = new CheckAuthRequest();

        Cookie[] cookies = request.getCookies();
        if (cookies != null){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("access_token")&&!cookie.getValue().isEmpty()){
                    authRequest.setToken(cookie.getValue());
                }
            }
        }

        CheckAuthResponse authResponse = loginService.auth(authRequest);
        if (authResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(authResponse.getUserinfo());
        }

        return new ResponseUtil<>().setErrorMsg(authResponse.getMsg());
    }
}
